package br.com.sigmah.controller.businessObjects;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import br.com.sigmah.controller.facade.GradeHorariaFacade;
import br.com.sigmah.controller.utils.GradeHorariaVO;
import br.com.sigmah.controller.utils.HoraVO;
import br.com.sigmah.controller.utils.VersaoVO;
import br.com.sigmah.controller.utils.enunsDominios.DiasEnum;
import br.com.sigmah.domain.entity.Dia;
import br.com.sigmah.domain.entity.Hora;
import br.com.sigmah.domain.entity.Versao;
import br.com.sigmah.domain.repository.CursoDataManager;
import br.com.sigmah.domain.repository.DiaDataManager;
import br.com.sigmah.domain.repository.HoraDataManager;
import br.com.sigmah.domain.repository.VersaoDataManager;

@Service("gradeFacade")
public class GradeHorariaBO implements GradeHorariaFacade {

	@Autowired
	private CursoDataManager cursoDataManager;

	@Autowired
	private VersaoDataManager versaoDataManager;
	
	@Autowired
	private HoraDataManager horaDataManager;
	
	@Autowired
	private DiaDataManager diaDataManager;

	@Override
	public GradeHorariaVO salvar(GradeHorariaVO gradeHorariaVO, List<HoraVO> listaHoraVO) {
		GradeHorariaVO gradeVO = new GradeHorariaVO();
		Versao versao = new  Versao();
		List<Dia> diasLista = new  ArrayList<Dia>();
		List<Hora> horasLista = new ArrayList<Hora>();

		if(!listaHoraVO.isEmpty()){
			for(HoraVO horaVO: listaHoraVO){
				Hora hora= new Hora();
				hora.setHora(horaVO.getHora()+":"+horaVO.getMinutos());
				hora = horaDataManager.persist(hora);
				horasLista.add(hora);
			}
		}
		if(gradeHorariaVO!=null){
			versao.setNomeDaVersao(gradeHorariaVO.getNome());
			//versao.setDias(diasLista);
			versao = versaoDataManager.persist(versao);
		}
		
		
		//if(versao.getId()!=null){
			for(DiasEnum dias: DiasEnum.listaDias()){
				Dia dia = new Dia();
				dia.setVersao(versao);
				dia.setNomeDia(dias.getDescricao());
				dia.setCodigoDia(dias.getCode());
				dia.setHoras(horasLista);
				diaDataManager.persist(dia);
			}
				
		return gradeVO;
	}

	public CursoDataManager getCursoDataManager() {
		return cursoDataManager;
	}

	public void setCursoDataManager(CursoDataManager cursoDataManager) {
		this.cursoDataManager = cursoDataManager;
	}

	public VersaoDataManager getVersaoDataManager() {
		return versaoDataManager;
	}

	public void setVersaoDataManager(VersaoDataManager versaoDataManager) {
		this.versaoDataManager = versaoDataManager;
	}

	public HoraDataManager getHoraDataManager() {
		return horaDataManager;
	}

	public void setHoraDataManager(HoraDataManager horaDataManager) {
		this.horaDataManager = horaDataManager;
	}

	public DiaDataManager getDiaDataManager() {
		return diaDataManager;
	}

	public void setDiaDataManager(DiaDataManager diaDataManager) {
		this.diaDataManager = diaDataManager;
	}


	
	

}
